#include<iostream>
#include<algorithm>		//是所有STL头文件中最大的一个，范围涉及到
						//比较，交换，查找，遍历操作，复制修改等等
#include<functional>	//定义了一些模板类，用于一些模板类
						//用以声明函数对象
#include<numeric>		//体积很小，只包括几个在序列上面进行
						//简单数学运算的模板函数
#include<vector>
using namespace std;





//遍历算法



/*
 //for_each		//遍历容器
//for_each(v.begin(), v.end(), _func)	//起始迭代器，结束迭代器，函数或仿函数
//普通函数
void print01(int val)
{
	cout << val << " ";
}

//仿函数
class print02
{
public:
	void operator() (int val)
	{
		cout << val << " ";
	}
};

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), print01);
	cout << endl;
	for_each(v.begin(), v.end(), print02());
	cout << endl;
}

int main()
{
	test01();
	return 0;
}
*/



/*
//transform		//搬运容器到另一个容器中
//transform(iterator beg1, iterator end1, iterator beg2, _func)
//原容器起始迭代器，原容器结束迭代器，新容器起始迭代器，函数或函数对象

class TransForm
{
public:
	int operator() (int v)
	{
		return v + 10;
	}
};

class MyPrint
{
public:
	void operator() (int val)
	{
		cout << val << " ";
	}

};

void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	
	vector<int> v2;
	v2.resize(v1.size());		//目标容器必须提前开辟空间
	transform(v1.begin(), v1.end(), v2.begin(), TransForm());

	for_each(v2.begin(), v2.end(), MyPrint());
	cout << endl;
}

int main()
{
	test01();
	return 0;
}
*/






//常用查找算法
/*
find()				//查找元素
find_if()			//按条件查找元素
adjacent_find()	//查找相邻重复元素
binary_search()	//二分查找法
count()			//统计元素个数
countif()			//按条件统计元素个数
*/





/*
//find(iterator beg, iterator end, vlaue)	//没找到返回.end()
//查找内置数据类型
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i);

	vector<int>::iterator it = find(v.begin(), v.end(), 5);
	if (it == v.end())
	{
		cout << "没有找到。" << endl;
	}
	else
	{
		cout << "找到: " << *it << endl;
	}
}

//查找自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}

	//重载==
	bool operator == (const Person& p)
	{
		if (this->m_Age == p.m_Age && this->m_Name == p.m_Name)
		{
			return true;
		}
		else
			return false;
	}

	string m_Name;
	int m_Age;
};

void test02()
{
	vector<Person> v;
	Person p1("张三", 10);
	Person p2("李四", 30);
	Person p3("王五", 40);
	Person p4("赵六", 20);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	Person pp("张三", 10);

	vector<Person>::iterator it = find(v.begin(), v.end(), pp);
	if (it == v.end())	//需要重载==
	{
		cout << "没有找到。" << endl;
	}
	else
	{
		cout << "找到:  姓名: " << it->m_Name << "\t年龄: " << it->m_Age << endl;
	}


}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
*/



//按条件查找元素
//find_if(iterator beg, iterator end, _pred)	
//起始迭代器，结束迭代器，函数或者谓词
//内置数据类型
/*
class GreaterFive
{
public:
	bool operator() (int val)
	{
		return val > 5;
	}
};
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());

	if (it == v.end())
	{
		cout << "没有找到。" << endl;
	}
	else
	{
		cout << "找到大于5的数字为: " << *it << endl;
	}
}

//自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}

	string m_Name;
	int m_Age;
};

class Greater20
{
public:
	bool operator()(Person& p)
	{
		return p.m_Age > 20;
	}
};

void test02()
{
	vector<Person> v;
	Person p1("赵云", 20);
	Person p2("张飞", 23);
	Person p3("刘备", 24);
	Person p4("关羽", 23);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	vector<Person>::iterator it =  find_if(v.begin(), v.end(), Greater20());

	if (it == v.end())
	{
		cout << "没有找到。" << endl;
	}
	else
	{
		cout << "找到姓名： " << it->m_Name << "\t年龄：" << it->m_Age << endl;
	}

}

int main()
{
	test01();
	test02();
	return 0;
}
*/




//查找相邻重复元素
//adjacent_find(iterator beg, iterator end);
//返回相邻元素的第一个位置的迭代器
/*
#include<algorithm>
void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(10);
	v.push_back(50);
	v.push_back(30);
	v.push_back(20);
	v.push_back(30);

	vector<int>::iterator it = adjacent_find(v.begin(), v.end());

	if (it == v.end())
	{
		cout << "未找到。" << endl;
	}
	else
	{
		cout << "找到相邻重复的元素: " << *it << endl;
	}
}

int main()
{
	test01();
	return 0;
}
*/



//二分查找法
//查找指定元素是否存在 查到返回true 否则返回false
//binary_search(iterator beg, iteator end, value);
//在无序序列中不可用
/*
#include<algorithm>
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	bool ret = binary_search(v.begin(), v.end(), 6);
	if (ret)
	{
		cout << "找到了元素。" << endl;
	}
	else
	{
		cout << "未找到。" << endl;
	}
}

int main()
{
	test01();
	return 0;
}
*/




//统计元素个数
//count(iterator beg, iterator end, value)
/*
//统计内置数据类型
void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);

	int num = count(v.begin(), v.end(), 30);

	cout << num << endl;
}

//统计自定义数据类型
#include<string>
class Person
{

public:
	bool operator==(const Person &p)
	{
		if (this->m_Age = p.m_Age)
		{
			return true;
		}
		else
			return false;
	}

	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	int m_Age;
	string m_Name;
};

void test02()
{
	vector<Person> v;

	Person p1("刘备", 48);
	Person p2("诸葛亮", 34);
	Person p3("赵云", 35);
	Person p4("张飞", 45);
	Person p5("关羽", 48);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	Person p("曹操", 48);

	int num = count(v.begin(), v.end(), p);
	cout << num << endl;
}

int main()
{
	test01();
	test02();
	return 0;
}
*/




//按条件统计元素个数
//count_if(iterator beg, iterator end, _Pred)	_Pred是谓词

/*
//统计内置数据类型
class MyCompare
{
public:
	bool operator()(int v1)
	{
		if (v1 >= 20)
		{
			return true;
		}
		else
			return false;
	}
};

void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	v.push_back(50);

	//统计大于20的数
	int num = count_if(v.begin(), v.end(), MyCompare());
	cout << num << endl;

}

//统计自定义数据类型
class Person
{
public:
	Person(int age, string name)
	{
		this->m_Age = age; 
		this->m_Name = name;
	}

	string m_Name;
	int m_Age;
};

class Greater20
{
public:
	bool operator()(const Person& p)
	{
		return p.m_Age > 20;
	}
};

void test02()
{
	vector<Person> v;
	
	Person p1(24, "关羽");
	Person p2(23, "赵云");
	Person p3(25, "刘备");
	Person p4(21, "张飞");
	Person p5(18, "张三");
	Person p6(19, "李四");
	Person p7(26, "王五");
	Person p8(19, "赵六");

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	v.push_back(p6);
	v.push_back(p7);
	v.push_back(p8);

	//统计年龄大于20的人数
	int num = count_if(v.begin(), v.end(), Greater20());
	cout << num << endl;
}

int main()
{
	test01();
	test02();
	return 0;
}
*/








//常用排序算法
//sort				//对容器内元素进行排序
//random_shuffle	//洗牌，指定范围内的元素随机调整次序
//merge				//容器元素合并，并储存到另一个容器中
//reverse			//反转指定范围内的元素


//sort(iterator beg, iterator end, _Pred)
//第三个谓词可以不填，默认升序
/*
void PrintVector(vector<int> v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void myPrint(int val)
{
	cout << val << " ";
}

class myCompare
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};

void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);

	//升序
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	//降序
	sort(v.begin(), v.end(), myCompare());
	PrintVector(v);
}

int main()
{
	test01();
	return 0;
}
*/




//random_shuffle(iterator beg, iterator end)
//指定范围内的元素随机调整次序
/*
void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;
}
int main()
{
	//需要加随机数种子
	srand((unsigned int)time(NULL));
	test01();
	return 0;
}
*/



//merge(iterator beg1,iterator end1, iterator beg1, iterator end2, dest)
//两个有序的容器合并为一个有序的容器,其中dest是目标容器开始迭代器
/*
void PrintVector(vector<int> v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

class myprint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test01()
{
	vector<int> v1;
	vector<int> v2;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 20);
	}

	vector<int> v3;

	//提前给目标内存分配空间
	v3.resize(v1.size() + v2.size());
	
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), v3.end(), myprint());
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}
*/




//将容器元素进行反转
//reverse(iterator beg, iterator end)

class myprint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	reverse(v.begin(), v.end());

	for_each(v.begin(), v.end(), myprint());
	cout << endl;
}
int main()
{
	test01();
	return 0;
}

































